﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace RegisterPage
{
    public partial class Form1 : Form
    {
        public static Action<string> showLog = null;
        string Txt_db = "";

        public Form1(string path)
        {
            InitializeComponent();
            showLog = LogAction;
            Txt_db = path;
        }

        public void LogAction(string txt)
        {
            richText_Log.Text += (txt + "\r\n");
        }

      

        /// <summary>
        /// 读取Dll
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, PurviewPage> GetIList()
        {
            Dictionary<string, PurviewPage> list = new Dictionary<string, PurviewPage>();
            Assembly assembly = Assembly.LoadFrom("yq.Controller.dll");
            Type[] ts = assembly.GetTypes();
            foreach (Type t in ts)
            {
                string  areaStr ="";
                string controllerStr="";
                MethodInfo[] methodInfo = t.GetMethods();

                if (t.ToString().StartsWith("yq.Controller.") && t.ToString().EndsWith("Controller"))
                {
                    //区域
                    areaStr = t.ToString().Split(char.Parse("."))[2];
   
                    if (list.ContainsKey(areaStr) == false)
                    {
                        PurviewPage page = new PurviewPage()
                        {
                            Path = areaStr,
                            Type  = yq.Enums.PurviewPage.Type.Area,
                            PwId = areaStr.ToUpper().ExtMD5(),
                            Name = areaStr,
                            IsOpen = yq.Enums.BooleanType.Yes 
                        };
                        list.Add(page.Path, page);
                        LogAction(page.Path);
                    }

                    //控制器
                    string  controllerName = t.ToString().Split(char.Parse("."))[3].Replace("Controller", "");
                    controllerStr = areaStr + "/" + controllerName;
                    if (list.ContainsKey(controllerStr) == false)
                    {
                        PurviewPage page = new PurviewPage()
                        {
                            Path = controllerStr,
                            Type = yq.Enums.PurviewPage.Type.Controller,
                            PwId = controllerStr.ToUpper().ExtMD5(),
                            Name = controllerName,
                            IsOpen = yq.Enums.BooleanType.Yes
                        };
                        list.Add(page.Path, page);
                        LogAction(page.Path);
                    }

                    Console.WriteLine(t);
                }
                else
                {
                    continue;
                }

                foreach (MethodInfo mi in methodInfo)
                {
                    if (mi.ToString().StartsWith("System.Web.Mvc.ActionResult"))
                    {
                        //方法
                        var methodName = mi.ToString().Replace(" ", "").Replace("System.Web.Mvc.ActionResult", "");
                        methodName = methodName.Substring(0, methodName.IndexOf(char.Parse("(")));
                        string methodStr = controllerStr + "/" + methodName;

                        if (list.ContainsKey(methodStr)==false)
                        {
                            PurviewPage page = new PurviewPage()
                            {
                                Path = methodStr,
                                Type = yq.Enums.PurviewPage.Type.Method,
                                PwId = methodStr.ToUpper().ExtMD5(),
                                Name = methodName,
                                 IsOpen= yq.Enums.BooleanType.No
                            };
                            list.Add(page.Path, page);
                            LogAction(page.Path);
                        }
                    }
                }
            }
            return list;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Dictionary<string, PurviewPage> list = GetIList();
            LogAction("读取到" + list.Count + "个文件");
            LogAction("准备开始对比写入");
            int count = PurviewPageDAL.Insert(list);
            LogAction("新增" + count + "条记录");
        }



    }
}
